package com.mingqijia.gassafety.console.migrate.handler;

import com.mingqijia.gassafety.console.migrate.config.MigrateConfig;
import com.mingqijia.gassafety.console.migrate.config.ThreadPoolConfig;
import com.mingqijia.gassafety.console.migrate.model.MigrateCompanyModel;
import com.mingqijia.gassafety.db.entity.MigrateConsumer;
import com.mingqijia.gassafety.db.entity.MigrateContacts;
import com.mingqijia.gassafety.db.entity.MigrateDtu;
import com.mingqijia.gassafety.db.mapper.ConsumerMapper;
import com.mingqijia.gassafety.db.mapper.ContactsMapper;
import com.mingqijia.gassafety.db.mapper.DtuMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @Description:
 * @Author: zero
 * @CreateTime: 2022-11-30
 */
@Component
@Slf4j
public class MigrateInsertHandler {

    @Resource
    private DtuMapper dtuMapper;

    @Resource
    private ConsumerMapper consumerMapper;

    @Resource
    private ContactsMapper contactsMapper;

    private Map<String, String> imeiMap = new HashMap<>();

    private volatile Integer iccId = 800000;

    @Async(ThreadPoolConfig.INERT_MIGRATE_ASYNC)
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertDB(MigrateConsumer consumer, List<MigrateContacts> contactsList, List<MigrateDtu> dtuList) throws Exception {
        Integer flag = consumerMapper.insertConsumer(consumer);
        if (flag < 1) {
            return false;
        }
        Integer consumerId = consumer.getId();
        if (contactsList != null) {
            for (MigrateContacts contacts : contactsList) {
                contacts.setConsumerId(consumerId);
                contacts.setSpId(consumer.getSpId());
            }
            contactsMapper.insertBatch(contactsList);
        }
        if (dtuList != null) {
            Map<String, String> dictionaryMap = MigrateConfig.dictionaryMap;
            Map<String, String> imeiMap = new HashMap<>();
            for (MigrateDtu dtu : dtuList) {
                if (dtu.getConsumerId() != 0) {
                    dtu.setConsumerId(consumerId);
                }
                dtu.setSpId(consumer.getSpId());
                dtu.setPath(consumer.getPath());
                // 2022-12-23 测试增加
                dtu.setInstallationAddress(consumer.getAddress());
                dtu.setPositionLon(consumer.getPositionLon());
                dtu.setPositionLat(consumer.getPositionLat());
                handlerDTU(dtu, dictionaryMap);
            }
            dtuMapper.insertMigrateBatch(dtuList);
        }
        return true;
    }

    @Async(ThreadPoolConfig.INERT_DTU_ASYNC)
    public Boolean insertDTUDB(List<MigrateDtu> dtuList, Map<String, MigrateCompanyModel> companyModelMap) {
        if (dtuList != null) {
            Map<String, String> dictionaryMap = MigrateConfig.dictionaryMap;

            for (MigrateDtu dtu : dtuList) {
                MigrateCompanyModel migrateCompanyModel = companyModelMap.get(dtu.getSpId());
                dtu.setSpId(migrateCompanyModel.getSpId());
                dtu.setPath(migrateCompanyModel.getPath());
                handlerDTU(dtu, dictionaryMap);
            }
            dtuMapper.insertMigrateBatch(dtuList);
        }
        return true;
    }

    private void handlerDTU(MigrateDtu dtu, Map<String, String> dictionaryMap) {
        if (!StringUtils.isEmpty(dtu.getIMEI())) {
            String IMEI = dtu.getIMEI();
            if (dtu.getIMEI().contains("-")) {
                IMEI = dtu.getIMEI().substring(0, dtu.getIMEI().indexOf("-"));
            }
            if (imeiMap.get(IMEI) == null) {
                ++iccId;
                String result = "86868686868686" + iccId;
                imeiMap.put(IMEI, result);
            }
            dtu.setIccid(imeiMap.get(IMEI));
        }else{
            ++iccId;
            dtu.setIccid("86868686868686" + iccId);
        }
        if (!StringUtils.isEmpty(dtu.getDetectionType()) && !dtu.getDetectionType().contains(";")) {
            dtu.setDetectionType(dtu.getDetectionType() + ";");
        }
        dtu.setEquipmentManufacturer(dictionaryMap.get(dtu.getEquipmentManufacturer()));
        dtu.setOperators(dictionaryMap.get(dtu.getOperators()));
        dtu.setCmpRegistered(1);
        dtu.setProtocolVersion("v1.0");
        dtu.setInstallationPosition(Optional.ofNullable(dtu.getInstallationPosition()).orElse("其他"));
    }

}
